import 'package:flutter/material.dart';
import 'package:flutter_sensor_backend/assets/color.dart';
import 'package:flutter_sensor_backend/component/myleading.dart';
import 'package:flutter_sensor_backend/component/niw.dart';
import 'package:flutter_sensor_backend/middleware/mid_interface.dart';

class DataPanel extends StatefulWidget {
  const DataPanel({super.key});

  @override
  State<DataPanel> createState() => _DataPanelState();
}

class _DataPanelState extends State<DataPanel> {
  List data = [];
  getData() async {
    var tmp = await Api().FilterData({});
    // print(tmp);
    try {
      data = tmp["data"]["result"];
    } catch (e) {}
    setState(() {});
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const MyLeading(),
        title: Text(
          "数据总览",
          style: TextStyle(
            color: os_black,
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      body: ListView(
        children: data.map((e) => DataPanelCard(data: e)).toList(),
      ),
    );
  }
}

class DataPanelCard extends StatefulWidget {
  var data;
  DataPanelCard({super.key, this.data});

  @override
  State<DataPanelCard> createState() => _DataPanelCardState();
}

class _DataPanelCardState extends State<DataPanelCard> {
  String getTimeFromStamp(int stamp) {
    // 时间戳转为日期 YYYY-mm-dd
    var date = DateTime.fromMillisecondsSinceEpoch(stamp);
    return date.toString().substring(0, 10);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(),
      margin: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      child: myInkWell(
        widget: Container(
          padding: EdgeInsets.symmetric(horizontal: 17.5, vertical: 15),
          child: Row(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Text(
                        "数据编号 " +
                            widget.data["Identical"].toString().substring(21),
                        style: TextStyle(
                          color: os_black,
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Container(width: 7.5),
                      Container(
                        child: Text(
                          widget.data["FallDown"] ? "跌倒" : "未跌倒",
                          style: TextStyle(
                            color: os_color,
                            fontSize: 13,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        padding:
                            EdgeInsets.symmetric(horizontal: 7, vertical: 3),
                        decoration: BoxDecoration(
                          color: os_color_opa,
                          borderRadius: BorderRadius.circular(5),
                        ),
                      ),
                      Container(width: 7.5),
                      Container(
                        child: Text(
                          widget.data["DeviceInfo"],
                          style: TextStyle(
                            color: os_color,
                            fontSize: 13,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        padding:
                            EdgeInsets.symmetric(horizontal: 7, vertical: 3),
                        decoration: BoxDecoration(
                          color: os_color_opa,
                          borderRadius: BorderRadius.circular(5),
                        ),
                      ),
                    ],
                  ),
                  Container(height: 10),
                  Text("采集时间：" + getTimeFromStamp(widget.data["Timestamp"])),
                ],
              ),
              // Text(widget.data.toString()),
            ],
          ),
        ),
        radius: 10,
      ),
    );
  }
}
